#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <goat3dgfx/goat3dgfx.h>
#include <vmath/vmath.h>

using namespace goatgfx;

static bool init();
static void cleanup();
static void display();
static void reshape(int x, int y);
static void keyboard(unsigned char key, int x, int y);
static void mouse(int bn, int st, int x, int y);
static void motion(int x, int y);
static bool parse_args(int argc, char **argv);

static float cam_theta, cam_phi, cam_dist = 8;
static const char *scene_filename;
static Scene *scn;

static ShaderProg *sdr;

int main(int argc, char **argv)
{
	glutInit(&argc, argv);
	glutInitWindowSize(800, 600);
	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
	glutCreateWindow("viewscn");

	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(keyboard);
	glutMouseFunc(mouse);
	glutMotionFunc(motion);

	if(!parse_args(argc, argv)) {
		return 1;
	}

	if(!init()) {
		return 1;
	}
	atexit(cleanup);

	glutMainLoop();
	return 0;
}

static bool init()
{
	glewInit();

	glClearColor(0.1, 0.1, 0.1, 1);

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	if(!(sdr = get_sdrprog("sdr/foo.v.glsl", "sdr/foo.p.glsl"))) {
		return false;
	}

	scn = new Scene;
	if(!scn->load(scene_filename)) {
		fatal_log("failed to load scene: %s\n", scene_filename);
		return false;
	}

	return true;
}

static void cleanup()
{
	delete scn;
}

static void display()
{
	glClearColor(0.25, 0.25, 0.25, 1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	Matrix4x4 view_matrix;
	view_matrix.translate(Vector3(0, 0, -cam_dist));
	view_matrix.rotate(Vector3(1, 0, 0), M_PI * cam_phi / 180.0);
	view_matrix.rotate(Vector3(0, 1, 0), M_PI * cam_theta / 180.0);
	set_view_matrix(view_matrix);

	//setup_gl_matrices();

	goatgfx::bind_shader(sdr);
	scn->draw();
	goatgfx::bind_shader(0);

	/*glBegin(GL_QUADS);
	glColor3f(1, 1, 1);
	glVertex3f(-1, -1, 0);
	glVertex3f(1, -1, 0);
	glVertex3f(1, 1, 0);
	glVertex3f(-1, 1, 0);
	glEnd();*/

	glutSwapBuffers();
	CHECKGLERR;
}

static void reshape(int x, int y)
{
	glViewport(0, 0, x, y);

	Matrix4x4 proj;
	proj.set_perspective(M_PI / 4.0, (float)x / (float)y, 0.5, 500.0);
	set_projection_matrix(proj);
}

static void keyboard(unsigned char key, int x, int y)
{
	switch(key) {
	case 27:
		exit(0);
	}
}

static bool bnstate[16];
static int prev_x, prev_y;

static void mouse(int bn, int st, int x, int y)
{
	bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
	prev_x = x;
	prev_y = y;
}

static void motion(int x, int y)
{
	int dx = x - prev_x;
	int dy = y - prev_y;
	prev_x = x;
	prev_y = y;

	if(!dx && !dy) return;

	if(bnstate[0]) {
		cam_theta += dx * 0.5;
		cam_phi += dy * 0.5;
		cam_phi = std::max(-90.0f, std::min(90.0f, cam_phi));
		glutPostRedisplay();
	}
}

static bool parse_args(int argc, char **argv)
{
	for(int i=1; i<argc; i++) {
		if(argv[i][0] == '-' && argv[i][2] == 0) {
			switch(argv[i][1]) {
			case 'h':
			default:
				printf("usage: %s <filename>\n", argv[i]);
				exit(0);
			}
		} else {
			if(scene_filename) {
				fprintf(stderr, "unexpected argument: %s\n", argv[i]);
				return false;
			}
			scene_filename = argv[i];
		}
	}

	if(!scene_filename) {
		fprintf(stderr, "you must specify the scene file to view\n");
		return false;
	}

	return true;
}
